package org.opens;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.io.IoUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import org.opens.excel.entity.CourseEntity;
import org.opens.excel.entity.StudentEntity;
import org.opens.excel.entity.TeacherEntity;
import org.opens.util.EntityRandomUtil;
import org.opens.util.PathUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class One2ManyExport {

    @Test
    public void test1() throws InstantiationException, IllegalAccessException {
        List<CourseEntity> courseList = EntityRandomUtil.getList(CourseEntity.class, 2);

        List<TeacherEntity> teacherList1 = EntityRandomUtil.getList(TeacherEntity.class, 1);
        List<TeacherEntity> teacherList2 = EntityRandomUtil.getList(TeacherEntity.class, 1);

        List<StudentEntity> studentList1 = EntityRandomUtil.getList(StudentEntity.class, 3);
        List<StudentEntity> studentList2 = EntityRandomUtil.getList(StudentEntity.class, 3);

        courseList.get(0).setMathTeacher(teacherList1.get(0));
        courseList.get(1).setMathTeacher(teacherList2.get(0));

        courseList.get(0).setStudents(studentList1);
        courseList.get(1).setStudents(studentList2);

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生", "学生"), CourseEntity.class, courseList);

        FileOutputStream fileOutputStream = null;

        try {
            String currentClassPath = PathUtil.getCurrentClassRunPath();
            File studentEntityClassPathFile = new File(currentClassPath, "One2Many.xls");
            fileOutputStream = new FileOutputStream(studentEntityClassPathFile);

            workbook.write(fileOutputStream);
        } catch (IOException e) {
            //
        } finally {
            IoUtil.close(fileOutputStream);
            IoUtil.close(workbook);
        }
    }

}
